<template>
	<view class="warp">
		<!-- #ifdef APP-PLUS -->
		<view class="title-contents">
			<view class="top-view status"></view>
		</view>
		<!-- #endif -->
		<!-- 密码登录 -->
		<view class="login-phone form-wapper">
			<view class="phone input-wapper">
				<view class="lside"><text class="iconfont icon-shouji"></text></view>
				<view class="mobile rside"><input v-model="username" placeholder-class="input-placeholder" type="number"
						placeholder="请输入手机号" /></view>
			</view>
			<view class="phone input-wapper">
				<view class="lside"><text class="iconfont icon-yanzhengma"></text></view>
				<view class="mobile rside"><input v-model="vercode" placeholder-class="input-placeholder" type="number"
						placeholder="请输入验证码" /></view>

				<view>
					<button type="default" @click="sendSms">获取验证码</button>
				</view>
			</view>
			<view class="code input-wapper">
				<view class="lside"><text class="iconfont icon-mima2"></text></view>
				<view class="rside"><input v-model="password" placeholder-class="input-placeholder" type="password"
						maxlength="12" placeholder="请设置新密码(6-12位) " /></view>
			</view>
		</view>

		<button type="primary" class="login-btn" @click="forget">确定</button>
	</view>
</template>
<script>
export default {
	data() {
		return {
			requt: 'https://4de16698-d9f4-443a-b416-ef1037cce952.bspapp.com/http',
			user: [],
			show: true,
			username: "",
			password: "",
			vercode: "",
			codeBtnDisabled: false,
			code_text: "发送验证码",
		};
	},
	onLoad() {
		// this.Reg()
	},
	methods: {
		//忘记密码接口
		forget() {
			let vvercode = this.vercode
			uni.request({
				url: this.requt + '/user/forget',
				method: "POST",
				data: {
					username: this.username,
					password: this.password,
					vercode: vvercode
				},
				success: res => {
					console.log(res);
					if (res.data.code == 0) {
						console.log('修改成功请重新登录')
						uni.naigateTo({
							url: '/pages/public/login'
						})
					}
				},
				fail: err => {
					console.error(err);
					if (res.data.code == 1) {
						uni.$u.toast('已有帐号，请去登陆')
						console.log("已有帐号，请去登陆")
						uni.naigateTo({
							url: '/pages/public/login'
						})
					}
				}
			});
		},
		//点击发送验证码
		sendSms() {
			uni.request({
				url: this.requt + '/user/sendSms',
				method: "POST",
				data: {
					mobile: this.username,
					type: " login",
				},
				success: res => {
					console.log(res);
					console.log(this.username)
					if (res.data.code == 0) {
						//发送成功
						console.log(`短信已发送到手机号${this.username},请注意查收`)

						// 验证码 倒计时
						let count = 15;
						let timer = setInterval(() => {
							count--;
							this.code_text = `${count}s后再次获取`;

							if (count <= 0) {
								clearInterval(timer);
								this.code_text = `再次获取`;
								this.codeBtnDisabled = false;
								count = 60;
							}
						}, 1000);
					} else {
						uni.$u.toast(res.msg || "发送失败");
						this.codeBtnDisabled = false;
						this.code_text = "再次发送";
					}
				},
				fail: err => {
					console.log(err);
					uni.$u.toast(err.msg || "发送失败");
					this.codeBtnDisabled = false;
					this.code_text = "再次发送";
				}
			});
		},
	},
};

</script>
<style lang="scss">
@import "login.scss"
</style>
